package cn.yshujia.controller;

import cn.yshujia.domain.entity.Classify;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.service.impl.ClassifyServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 控制器
 */

@RestController
@Tag (name = "Classify", description = "分类Api")
public class ClassifyController {
	
	@Resource
	ClassifyServiceImpl service;
	
	@GetMapping ("/classify/{id}")
	@Operation (summary = "根据 分类ID 获取分类")
	public ApiVO<Classify> list(@PathVariable Long id) {
		return ApiVO.success(service.selectById(id));
	}
	
	@GetMapping ("/admin/classify/list")
	@Operation (summary = "admin 获取分类列表")
	@PreAuthorize ("@sys.hasOnePermission('classify:all')")
	public ApiVO<List<Classify>> list() {
		return ApiVO.success(service.getList());
	}
	
	@PostMapping ("/admin/classify/insert")
	@Operation (summary = "admin 添加分类")
	@PreAuthorize ("@sys.hasOnePermission('classify:all')")
	public ApiVO<Boolean> insert(@RequestBody Classify classify) {
		boolean bool = service.insert(classify);
		if (bool) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@PutMapping ("/admin/classify/update")
	@Operation (summary = "admin 更新分类")
	@PreAuthorize ("@sys.hasOnePermission('classify:all')")
	public ApiVO<Boolean> update(@RequestBody Classify classify) {
		boolean result = service.update(classify);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
	@DeleteMapping ("/admin/classify/delete")
	@Operation (summary = "admin 删除分类")
	@PreAuthorize ("@sys.hasOnePermission('classify:all')")
	public ApiVO<Boolean> remove(@RequestBody List<Long> ids) {
		boolean result = service.remove(ids);
		if (result) {
			return ApiVO.success(true);
		}
		return ApiVO.error();
	}
	
}

